package com.answerz.module_reportissues.viewmodel

import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.DataRepository
import com.czl.base.event.LiveBusCenter
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriberHelper
import com.czl.base.util.RxThreadHelper

/**
 *
 * @Description:
 * @Author: XCH
 * @CreateDate: 2021/12/9 11:11
 */
class ServiceScoreViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {

    var no = ""
    var evaluatedId = ""
    var evaluatedName = ""
    var comment = ObservableField("")
    var score = ObservableInt(5)
    val uc = UiChangeEvent()
    val commentType = ObservableField(AppConstants.Constants.COMMENT_TYPE_ORDER)

    inner class UiChangeEvent {
        val evaluateCompleteEvent: SingleLiveEvent<Void> = SingleLiveEvent()
    }

    val onCommentChangeCommand: BindingCommand<String> = BindingCommand(BindingConsumer {
        comment.set(it)
    })

    var onQueryClick: BindingCommand<Void> = BindingCommand(BindingAction {
        evaluateOrder()
    })
    var onRatingBarChangedClick: BindingCommand<Int> = BindingCommand(BindingConsumer {
        score.set(it)
    })

    private fun evaluateOrder() {
        var ob = when (commentType.get()) {
           AppConstants.Constants.COMMENT_TYPE_HOUSE -> model.evaluateHouseKeeper(
                mapOf(
                    "content" to comment.get().toString(),
                    "score" to score.get().toString(),
                    "evaluatedId" to evaluatedId,
                    "evaluatedName" to evaluatedName,
                    "areaNo" to model.getAreaId(),
                )
            )
            AppConstants.Constants.COMMENT_TYPE_COMPLAINT ->model.evaluateComplaint(
                mapOf(
                    "comment" to comment.get().toString(),
                    "score" to score.get().toString(),
                    "no" to no
                )
            )
            else ->model.evaluateOrder(
                mapOf(
                    "comment" to comment.get().toString(),
                    "score" to score.get().toString(),
                    "no" to no
                )
            )
        }
        ob.compose(RxThreadHelper.rxSchedulerHelper(this@ServiceScoreViewModel))
            .doOnSubscribe { showLoading() }
            .subscribe(object : ApiSubscriberHelper<BaseBean<Any?>>() {
                override fun onResult(t: BaseBean<Any?>) {
                    dismissLoading()
                    if (t.code == 200) {
                        when (commentType.get()) {
                            AppConstants.Constants.COMMENT_TYPE_HOUSE -> LiveBusCenter.postHouseKeeperRefreshEvent("")
                        }
                        uc.evaluateCompleteEvent.call()
                    } else {
                        showErrorToast(t.msg)
                    }
                }

                override fun onFailed(msg: String?) {
                    dismissLoading()
                    showErrorToast(msg)
                }

            })
    }
}